class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums)
    {
        int n = nums.size();
        int f = nums[0], g = 0;
        int summax = f, summin = 0, sum = nums[0];
        for (int i = 1; i < n; i++)
        {
            sum += nums[i];
            f = max(f, 0) + nums[i];
            g = min(g, 0) + nums[i];
            summax = max(summax, f);
            summin = min(summin, g);
        }
        return max(summax, sum - summin);
    }
};